package com.fzu.mmdbproxyframework.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

public class Tool {
	
	public static void main(String[] args) {
		System.out.println("caonima ");
	}
	
	public static String getlocalhostIp(){
		String INSTANCE_IP;
		if(System.getProperties().getProperty("os.name").startsWith("Windows"))
		{
			return INSTANCE_IP = getWindLocalHost_IP();
		}else{
			return INSTANCE_IP = getLinuxHostIP();
		}
		
	}

	
	public static String setInsertTime(){
		Date d = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sf.format(d);///2014-07-07 15:28:
		return time;
	}
	public  String getLocalHostName(){
		InetAddress addr;
		String LocalHost_Name = "";
		try {
			addr = InetAddress.getLocalHost();			
			LocalHost_Name=addr.getHostName().toString();//获得本机名称
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			System.out.println("UnknownHostException!!");
			e.printStackTrace();
		}	
		return LocalHost_Name;
		
	}
	public static String getWindLocalHost_IP()
	{
		InetAddress addr;
		String LocalHost_IP = "";
		try {
			addr = InetAddress.getLocalHost();
			LocalHost_IP=addr.getHostAddress().toString();//获得本机IP			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			System.out.println("UnknownHostException!!");
			e.printStackTrace();
		}	
		return LocalHost_IP;
	}
	public static String getLinuxHostIP()  {
	
		Enumeration allNetInterfaces;
		InetAddress ip ;
		String LinuxHostIP=null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		
			while (allNetInterfaces.hasMoreElements())
			{
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
	//			System.out.println("netInterface.getName()="+netInterface.getName());
				if(netInterface.getName().equals("eth0")){
					Enumeration addresses = netInterface.getInetAddresses();
					while (addresses.hasMoreElements())
					{
						ip = (InetAddress) addresses.nextElement();
						
						if (ip != null && ip instanceof Inet4Address)
						{
	//						System.out.println("InetAddress.IP = " + ip.getHostAddress());
							LinuxHostIP = ip.getHostAddress();
						} 
					}
				}
				
			}
			} catch (SocketException e) {
		
			System.out.println("InterruptedException");
			e.printStackTrace();
			}	
		return LinuxHostIP;	
	}
	
	public static void destroyDriver() {  
    	Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            try {
                DriverManager.deregisterDriver(driver);            
                
            } catch (SQLException e) {
            	e.printStackTrace();
            }
               
        }       
	}
}
